package 注解学习.Demo;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.reflect.Method;

@Check(className = "注解学习.Demo.Calculator")
@SuppressWarnings("all")
public class CheckTest {
    public static void main(String[] args) throws Exception {
        Class<CheckTest> checkTestClass = CheckTest.class;
        Check check = checkTestClass.getAnnotation(Check.class);

        String s = check.className();
        if (!" ".equals(s)) {
            int number = 0;
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("bug.txt"));
            Class aClass = Class.forName(s);
            //创建对象
            Object o = aClass.newInstance();

            /* //创建计算机对象
            Calculator c = new Calculator();
            //获取class对象
            Class aClass = c.getClass();*/
            //获取所有方法
            Method[] methods = aClass.getMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(Check.class)) {
                    //这个判断条件是判断是否有Check.class这个注解
                    //有就会进入到我们的method ;
                    try {
                        method.invoke(o);
                    } catch (Exception e) {
                        //捕获异常
                        number++;
                        bufferedWriter.write(method.getName() + "方法出异常了");
                        bufferedWriter.newLine();
                        bufferedWriter.write("异常名称" + e.getCause().getClass().getSimpleName());
                        //这个是打印出异常的名称
                        bufferedWriter.newLine();
                        bufferedWriter.write("异常的原因是" + e.getCause().getMessage());
                        bufferedWriter.newLine();
                        bufferedWriter.write("----------------");
                    }
                }
            }
            System.out.println("运行完成");
            bufferedWriter.write("本次运行共出现" + number + "次异常");
            bufferedWriter.flush();
            bufferedWriter.close();
        } else {
            System.out.println("运行失败,未输入对应测试用全类名");
        }


    }
}
